﻿using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using NetTao.Config;
using NetTao.Model;

namespace NetTao.Analyser
{
    public class AnalyseBase
    {
        protected string Content { get; private set; }

        public void SetContent(string content)
        {
            Content = content;
        }

        public string GetContent()
        {
            return Content;
        }

        private RuleEntity _rule;
        public RuleEntity Rule
        {
             get { return _rule ?? (_rule = new RuleEntity()); }
            set { _rule = value; }
        }

        private ArticleInfo _article;
        public ArticleInfo Article 
        {
            get { return _article ?? (_article = new ArticleInfo()); }
            set { _article = value; }
        }

        public Encoding Encode
        {
            get { return Encoding.GetEncoding(Rule.GetSiteCharset.Pattern); }
        }

        protected string MatchSingleValue(RulePara rulePara)
        {
            var match = Regex.Match(Content, rulePara.Pattern, rulePara.Options);
            var result = match.Groups[1].Value;
            if(!string.IsNullOrEmpty(rulePara.FilterPattern))
            {
                string[] arr = rulePara.FilterPattern.Split('♂');
                result = arr.Aggregate(result, (current, s) => current.Replace(s, ""));
            }
            return result;
        }
    }
}
